package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetFeastReward;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetFeastRewardDaoImpl extends TempDataObjectDao<UserBanquetFeastReward> {

    public Map<Integer, UserBanquetFeastReward> getUserBanquetFeastRewardMap(String banquetId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_feast_reward where userId = ? and `banquetId` = ?";
        Map<Integer, UserBanquetFeastReward> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setString(i++, banquetId);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetFeastReward data = parse(rs);
                res.put(data.getRewardId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean updateData(UserBanquetFeastReward data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_banquet_feast_reward SET isGetReward=?,getTime=?,reward=? where userId = ? and banquetId=? and rewardId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setBoolean(i++, data.isGetReward());
            call.setLong(i++, data.getGetTime());
            call.setString(i++, data.getReward());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getRewardId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean addData(UserBanquetFeastReward data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_banquet_feast_reward(banquetId,rewardId,userId,`type`,isGetReward,getTime,reward) values(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getBanquetId());
            call.setInt(i++, data.getRewardId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            call.setBoolean(i++, data.isGetReward());
            call.setLong(i++, data.getGetTime());
            call.setString(i++, data.getReward());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetFeastReward parseData(ResultSet rs) throws SQLException {
        UserBanquetFeastReward data = new UserBanquetFeastReward();
        data.setBanquetId(rs.getString("banquetId"));
        data.setRewardId(rs.getInt("rewardId"));
        data.setType(rs.getInt("type"));
        data.setUserId(rs.getLong("userId"));
        data.setGetReward(rs.getBoolean("isGetReward"));
        data.setGetTime(rs.getLong("getTime"));
        data.setReward(rs.getString("reward"));
        return data;
    }
}
